﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PromoServer.Library.Data;
using PromoServer.Library.Entities;

namespace PromoServer.Library.BLL
{
    public class PSLayout
    {
        #region Properties

        private PSData _psData;

        #endregion

        public PSLayout()
        {
            _psData = new PSData();
        }

        public List<Layout> GetLayouts()
        {
            try
            {
                List<Layout> lstLayouts;
                lstLayouts = _psData.GetLayouts();
                return lstLayouts;
            }
            catch (Exception ex)
            {
                // Log error
                PromoLog promoLog = new PromoLog();
                promoLog.Log("PSLayout.GetLayouts", ex.Message, true);
                return null;
            }
        }

        public int SaveLayout(Layout layout)
        {
            try
            {
                // If layout is new, add header record and return LayoutID else update header record
                if (layout.LayoutID == 0)
                    layout.LayoutID = _psData.AddLayoutHeader(layout);
                else
                    _psData.UpdateLayoutHeader(layout);

                // Add/update layout content
                foreach (LayoutContent content in layout.Content)
                {
                    // If layout is new, set layoutID of content
                    if (content.LayoutID == 0)
                        content.LayoutID = layout.LayoutID;

                    if (content.ID == 0)
                        content.ID = _psData.AddLayoutDetail(content);
                    else
                        _psData.UpdateLayoutDetail(content);
                }

                return layout.LayoutID;
            }
            catch (Exception ex)
            {
                // Log error
                PromoLog promoLog = new PromoLog();
                promoLog.Log("PSLayout.SaveLayout", ex.Message, true);
                return -1;
            }
        }

        public void SaveLayoutContents(List<LayoutContent> contents)
        {
            try
            {
                // Add/update layout content
                foreach (LayoutContent content in contents)
                {
                    if (content.ID == 0)
                        content.ID = _psData.AddLayoutDetail(content);
                    else
                        _psData.UpdateLayoutDetail(content);
                }
            }
            catch (Exception ex)
            {
                // Log error
                PromoLog promoLog = new PromoLog();
                promoLog.Log("PSLayout.SaveLayoutContents", ex.Message, true);
            }

        }

        public void SaveLayoutContent(LayoutContent content)
        {
            try
            {
                // Add/update layout content
                if (content.ID == 0)
                    content.ID = _psData.AddLayoutDetail(content);
                else
                    _psData.UpdateLayoutDetail(content);
            }
            catch (Exception ex)
            {
                // Log error
                PromoLog promoLog = new PromoLog();
                promoLog.Log("PSLayout.SaveLayoutContent", ex.Message, true);
            }
        }

        public string GetLayoutContentFileName(int id)
        {
            try
            {
                // Add/update layout content
                string contentFileName = _psData.GetLayoutContentFileName(id);
                return contentFileName;
            }
            catch (Exception ex)
            {
                // Log error
                PromoLog promoLog = new PromoLog();
                promoLog.Log("PSLayout.SaveLayoutContent", ex.Message, true);
                return string.Empty;
            }
        }

        public bool DeleteLayout(Layout layout)
        {
            try
            {
                // Delete layout
                bool result = _psData.DeleteLayout(layout);
                return result;
            }
            catch (Exception ex)
            {
                // Log error
                PromoLog promoLog = new PromoLog();
                promoLog.Log("PSLayout.DeleteLayout", ex.Message, true);
                return false;
            }
        }

        public bool DeleteLayoutContent(LayoutContent layout)
        {
            try
            {
                // Delete layout
                bool result = _psData.DeleteLayoutDetail(layout);
                return result;
            }
            catch (Exception ex)
            {
                // Log error
                PromoLog promoLog = new PromoLog();
                promoLog.Log("PSLayout.DeleteLayoutContent", ex.Message, true);
                return false;
            }
        }

        public bool UpdateLayoutSchedule(Layout layout, int scheduleID)
        {
            try
            {
                // Update layout schedule
                bool result = _psData.UpdateLayoutSchedule(layout, scheduleID);
                return result;
            }
            catch (Exception ex)
            {
                // Log error
                PromoLog promoLog = new PromoLog();
                promoLog.Log("PSLayout.UpdateLayoutSchedule", ex.Message, true);
                return false;
            }
        }
    }
}
